package com.example.sm.mycook.adapter;

import android.content.Context;
import android.content.Intent;
import android.graphics.Paint;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.bumptech.glide.Glide;
import com.example.sm.mycook.R;
import com.example.sm.mycook.activity.HomeComboActivity;
import com.example.sm.mycook.activity.HomeFriendActivity;
import com.example.sm.mycook.activity.HomeGiftActivity;
import com.example.sm.mycook.activity.HomeItemActivity;
import com.example.sm.mycook.activity.HomeTeamActivity;
import com.example.sm.mycook.activity.HomeVpActivity;
import com.example.sm.mycook.beans.HomeMyRVBean;
import com.example.sm.mycook.beans.HomeRVBean;
import com.example.sm.mycook.beans.HomeVpBean;
import com.example.sm.mycook.interfaceCallBack.HomeHeadCallBack;

import java.util.ArrayList;
import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

/**
 * Created by Administrator on 2016/12/6.
 */
public class HomeRVAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private Context context;
//    private List<HomeRVBean.DataBean.ActsBean.ItemsBean> list;
    private List<HomeMyRVBean> list;

    private View view;
    private RecyclerView recyclerView;
    private final int GV_ID=0;
    private final int LV_ID=1;
    private final int TV_ID=2;
    private final int VP_ID=3;
    private final int HEAD_GV=4;
    private int Ttype;private String img;
    private OnItemClickCallBack onItemClickCallBack;

    private HomeVpBean.DataBean dataBean;
    private List<View> imageViewList=new ArrayList<>();
    private List<String> stringList=new ArrayList<String>(){};

    public void setOnItemClickCallBack(OnItemClickCallBack onItemClickCallBack)
    {
        this.onItemClickCallBack=onItemClickCallBack;
    }

    public  interface  OnItemClickCallBack
    {
        void OnItemClickListener(View view ,int pos);
    }

    @Override
    public int getItemViewType(int position) {

        Log.e("AAAAAA", "-------->获取item类型");

        if (position>1) {
            String tType = list.get(position).gettType();
            Ttype = Integer.parseInt(tType);
        }
        if (position==0)
        {
//            return  VP_ID;
//        }else if (position==1)
//        {
          return HEAD_GV;
        }else
        if (Ttype==5)
        {
            return  TV_ID;
        }else if (Ttype==7)
        {
            return  GV_ID;

        }else {
            return LV_ID;
        }

    }

    public HomeRVAdapter(Context context, List<HomeMyRVBean> list) {
        this.context = context;
        this.list = list;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        RecyclerView.ViewHolder viewHolder=null;
        if (viewType==4)
        {
            view=LayoutInflater.from(context).inflate(R.layout.home_head_gv,parent,false);
            HeadGvHolder headGvHolder=new HeadGvHolder(view);
            viewHolder=headGvHolder;

        }
// else  if(viewType==3)
//        {
//            view=LayoutInflater.from(context).inflate(R.layout.home_vp_item,parent,false);
//            ViewPagerHolder viewPagerHolder=new ViewPagerHolder(view);
//            viewHolder=viewPagerHolder;
//
//        }else
       if (viewType==2)
       {
           view=LayoutInflater.from(context).inflate(R.layout.home_tv_iv,parent,false);
           MyTextHolder myTextHolder=new MyTextHolder(view);
           viewHolder=myTextHolder;
       }else if (viewType==0)
       {
            view=LayoutInflater.from(context).inflate(R.layout.home_rv_gv,parent,false);
           MyGVHolder gvHolder=new MyGVHolder(view);
           viewHolder=gvHolder;
       }
       else  if (viewType==1){
           view = LayoutInflater.from(context).inflate(R.layout.home_rv_item, parent, false);
           MyHolder myHolder = new MyHolder(view);
           viewHolder=myHolder;
       }

        view.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int pos=recyclerView.getChildAdapterPosition(v);

                if (pos!=0) {
                    onItemClickCallBack.OnItemClickListener(v, pos);
                }
            }
        });

        Log.e("AAAAAA","获取MyHolder");

        return  viewHolder;
    }

    @Override
    public void onBindViewHolder(final RecyclerView.ViewHolder holder, final int position) {

        if (position>0)
        {
            img=list.get(position).getImgUrl();
        }


        Log.e("AAAAAA","图片下载地址是："+img);
        if (!TextUtils.isEmpty(img))
        {
            if (holder instanceof  MyHolder)
            {
                Glide.with(context).load(img).into(((MyHolder) holder).iv);
            }
           else  if (holder instanceof MyTextHolder)
            {
                Glide.with(context).load(img).into(((MyTextHolder) holder).iv);

            }else if (holder instanceof MyGVHolder)
            {
                Glide.with(context).load(img).into(((MyGVHolder) holder).iv);
                ((MyGVHolder) holder).content.setText(list.get(position).getGoodsName());
                String oldPrice=list.get(position).getMarketPrice();
                String price=list.get(position).getPrice();
                if (oldPrice.equals(price))
                {
                    ((MyGVHolder) holder).price.setText("￥"+price);
                }else {
                    ((MyGVHolder) holder).oldPrice.setText("￥"+oldPrice);
                    ((MyGVHolder) holder).oldPrice.getPaint().setFlags(Paint.STRIKE_THRU_TEXT_FLAG);
                    ((MyGVHolder) holder).price.setText("￥"+price);
                }

                ((MyGVHolder) holder).iv.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        Toast.makeText(context,"你点击了图片",Toast.LENGTH_SHORT).show();

                    }
                });

                ((MyGVHolder) holder).addBtn.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        Toast.makeText(context,"你点击了添加的按钮",Toast.LENGTH_SHORT).show();

                    }
                });





            }


        }

//进行头部的处理
        //http://api5.wochu.cn/client/v1/app/layoutamend?parameters=%7B%22source%22%3A%22A%22%2C%22version%22%3A%224.2.0%22%7D

        Retrofit retrofit=new Retrofit.Builder()
                .baseUrl("http://api5.wochu.cn/client/v1/")
                .addConverterFactory(GsonConverterFactory.create())
                .build();

        HomeHeadCallBack homeHeadCallBack=retrofit.create(HomeHeadCallBack.class);

        homeHeadCallBack.getData().enqueue(new Callback<HomeVpBean>() {
            @Override
            public void onResponse(Call<HomeVpBean> call, Response<HomeVpBean> response) {
                dataBean=response.body().getData();

                if (holder instanceof  HeadGvHolder)
                {
                    List<HomeVpBean.DataBean.RecommendedContentBean> contentBeanList=dataBean.getRecommendedContent();

                    HomeVpBean.DataBean.RecommendedContentBean contentBean=contentBeanList.get(0);

                    List<HomeVpBean.DataBean.RecommendedContentBean.ItemsBean> itemsBeanList=contentBean.getItems();


                    Glide.with(context).load(itemsBeanList.get(0).getImgUrl()).into(((HeadGvHolder) holder).iv);
                    Glide.with(context).load(itemsBeanList.get(1).getImgUrl()).into(((HeadGvHolder) holder).iv2);
                    Glide.with(context).load(itemsBeanList.get(2).getImgUrl()).into(((HeadGvHolder) holder).iv3);
                    Glide.with(context).load(itemsBeanList.get(3).getImgUrl()).into(((HeadGvHolder) holder).iv4);

                  List<HomeVpBean.DataBean.CarouselBean> carouselBeanList=  dataBean.getCarousel();
                    for (int i = 0; i < 4; i++) {
                        ImageView imageView=new ImageView(context);
                        imageView.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, 150));
                        imageView.setScaleType(ImageView.ScaleType.FIT_XY);
                        String url=carouselBeanList.get(i).getPicUrl();
                       Glide.with(context).load(url).into(imageView);
                        imageViewList.add(imageView);
                    }
                    Log.e("AAA","ImageViewList========>"+imageViewList.size());

                    HomeHeadVpAdapter adapter=new HomeHeadVpAdapter(context,imageViewList);
                    ((HeadGvHolder) holder).vp.setAdapter(adapter);


                    ((HeadGvHolder) holder).iv.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            Intent intent=new Intent(context, HomeComboActivity.class);
                            context.startActivity(intent);

//                            Toast.makeText(context,"你点击了IV",Toast.LENGTH_SHORT).show();

                        }
                    });

                    ((HeadGvHolder) holder).iv2.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            Intent intent=new Intent(context, HomeTeamActivity.class);
                            context.startActivity(intent);
                            Toast.makeText(context,"你点击了IV2",Toast.LENGTH_SHORT).show();

                        }
                    });

                    ((HeadGvHolder) holder).iv3.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            Intent intent=new Intent(context, HomeGiftActivity.class);
                            context.startActivity(intent);
                            Toast.makeText(context,"你点击了IV3",Toast.LENGTH_SHORT).show();

                        }
                    });

                    ((HeadGvHolder) holder).iv4.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            Intent intent=new Intent(context, HomeFriendActivity.class);
                            context.startActivity(intent);
                            Toast.makeText(context,"你点击了IV4",Toast.LENGTH_SHORT).show();

                        }
                    });

                    stringList.add(0,"http://wmall.wochu.cn/h5/activity/html/tonicWinter2.html");
                    stringList.add(1,"bd9550be-da08-4760-b3d2-6600fa52eed7");
                    stringList.add(2,"http://wmall.wochu.cn/h5/activity/html/vegetables.html?v=1.2");
                    stringList.add(3,"http://wmall.wochu.cn/h5/activity/html/newDishe.html?v=1.2");

//                    ((HeadGvHolder) holder).vp.setOnTouchListener(new View.OnTouchListener() {
//                        @Override
//                        public boolean onTouch(View v, MotionEvent event) {
//                            int action=event.getAction();
//                            switch (action)
//                            {
//                                case MotionEvent.ACTION_DOWN:
//                                    int item=((HeadGvHolder) holder).vp.getCurrentItem();
//                                    String target=stringList.get(item);
//                                    if (item==2)
//                                    {
//                                        Intent intent=new Intent(context, HomeVpActivity.class);
//                                        intent.putExtra("target",target);
//                                        context.startActivity(intent);
//                                    }else {
//                                        Intent intent=new Intent(context, HomeItemActivity.class);
//                                        intent.putExtra("target",target);
//                                        context.startActivity(intent);
//                                    }
//
//                                    break;
//
//                            }
//
//                            return false;
//                        }
//                    });
                }



            }

            @Override
            public void onFailure(Call<HomeVpBean> call, Throwable t) {
                Log.e("AAAA","头部的数据下载失败");
            }
        });








    }

    @Override
    public int getItemCount() {
        Log.e("AAAA","------------->"+"集合大小"+list.size());
        return list!=null?list.size():0;
    }

    class MyHolder  extends RecyclerView.ViewHolder {
        ImageView iv;

        public MyHolder(View itemView) {
            super(itemView);
            iv= (ImageView) itemView.findViewById(R.id.home_rv_iv);
        }
    }

    class MyTextHolder extends RecyclerView.ViewHolder{

        ImageView iv;
        public MyTextHolder(View itemView) {
            super(itemView);
            iv= (ImageView) itemView.findViewById(R.id.home_tv_iv);
        }
    }

    class MyGVHolder extends RecyclerView.ViewHolder{

        ImageView iv,addBtn;
        TextView oldPrice,price,content;

        public MyGVHolder(View itemView) {
            super(itemView);
            addBtn= (ImageView) itemView.findViewById(R.id.homr_rv_gv_add);
            iv= (ImageView) itemView.findViewById(R.id.home_rv_gv_iv);
            content= (TextView) itemView.findViewById(R.id.home_rv_gv_content);
            oldPrice= (TextView) itemView.findViewById(R.id.home_rv_gv_oldPrice);
            price= (TextView) itemView.findViewById(R.id.home_rv_gv_price);
        }
    }


    class HeadGvHolder extends RecyclerView.ViewHolder{
        ImageView iv,iv2,iv3,iv4;
        ViewPager vp;
        public HeadGvHolder(View itemView) {
            super(itemView);
            vp= (ViewPager) itemView.findViewById(R.id.home_vp);
            iv= (ImageView) itemView.findViewById(R.id.home_head_gv_iv);
            iv2= (ImageView) itemView.findViewById(R.id.home_head_gv_iv2);
            iv3= (ImageView) itemView.findViewById(R.id.home_head_gv_iv3);
            iv4= (ImageView) itemView.findViewById(R.id.home_head_gv_iv4);
        }
    }

    @Override
    public void onAttachedToRecyclerView(RecyclerView recyclerView) {
        super.onAttachedToRecyclerView(recyclerView);
        this.recyclerView=recyclerView;
    }






}
